package cn.com.b2office.service.impl;

import java.io.Serializable;
import java.util.List;

import cn.com.b2office.dao.ExDataDao;
import cn.com.b2office.domain.ExData;
import cn.com.b2office.exchange.ClassMapping;
import cn.com.b2office.service.ExService;

public class ExServiceImpl implements ExService {

	private ExDataDao exDataDao = null;

	public void setExDataDao(ExDataDao exDataDao) {
		this.exDataDao = exDataDao;
	}

	public List<ExData> findExData(int size) {
		List<ExData> list = exDataDao.listExData(size);
		for (ExData data : list) {
			Class clasz = ClassMapping.getClass(data.getEntity());
			Serializable id = ClassMapping.getId(data.getEntity(), data
					.getEntityId());
			Object o = exDataDao.read(clasz, id);
			if (o != null)
				data.setObject((Serializable) o);
		}
		return list;
	}

	public ExData process(ExData data) {
		// echo
		ExData echo = new ExData();
		echo.setExId(data.getExId());
		echo.setEntity(ClassMapping.getClassCode(ExData.class));
		echo.setEntityId(String.valueOf(data.getExId()));
		echo.setOperation(ExData.DELETE);

		if (data.getOperation() != ExData.DELETE)
			exDataDao.saveOrUpdate(data.getObject());
		else if (data.getOperation() == ExData.DELETE) {
			Class clasz = ClassMapping.getClass(data.getEntity());
			Serializable id = ClassMapping.getId(data.getEntity(), data
					.getEntityId());
			if (clasz != null)
				exDataDao.delete(clasz, id);
		}

		return echo;
	}

}
